﻿using UnityEngine;
using static UnityEngine.InputSystem.InputAction;

public class Movement : MonoBehaviour
{
    public bool reverse = false;
    public float maxSpeed = 5f;
    public float acceleration = 1f;
    public float deceleration = 1f;
    public float curSpeed;
    public float dot;
    public Vector2 input;
    public Vector2 velocity;

    public void Move(CallbackContext context)
    {
        input = context.ReadValue<Vector2>();
        if(reverse)
        {
            input *= -1;
        }
    }

    void Update()
    {
        if (GameManager.Instance.IsGameOver) return;
        
        dot = Vector3.Dot(input, velocity);
        if(input == Vector2.zero || dot < 0)
        {
            curSpeed = Mathf.Lerp(curSpeed, 0, Time.deltaTime * deceleration);
        }
        else
        {
            curSpeed = Mathf.Lerp(curSpeed, maxSpeed, Time.deltaTime * acceleration);
        }
        velocity = (input + velocity).normalized * curSpeed;
        transform.Translate(Time.deltaTime * velocity);
    }

    public void SpeedUp()
    {
        maxSpeed += 1;
        acceleration += 1;
        deceleration += 1;
    }

    public void SpeedDown()
    {
        maxSpeed = Mathf.Max(maxSpeed -1,1);
        acceleration = Mathf.Max(acceleration - 0.5f, 0.5f);
        deceleration = Mathf.Max(deceleration - 0.5f, 0.5f);
    }
}
